package task

import (
	"github.com/gin-gonic/gin"
	"message-center/pkg/gconfig"
	"message-center/pkg/gerrno"
	"message-center/pkg/ginput"
	"message-center/pkg/gsms"
	"net/http"
)

/**
 * @api {POST} /api/v1/send/task/sms 短信消息
 * @apiVersion 1.0.0
 * @apiName  sms
 * @apiGroup  task
 * @apiDescription 发送短信
 * @apiExample {curl} Example usage:
 * curl -X POST -H "Content-Type:application/json" -d '{"to":"185167XXXXX", "signName":"开发测试", "templateCode":"SMS_XXXXX", "templateParams": "开发测试"}' -i http://localhost:9090/api/v1/send/task/sms
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": 200,
 *       "msg": "ok",
 *       "data": "发送成功！"
 *     }
 */
func SendSms(c *gin.Context) {
	var (
		rsp     = ginput.Gin{C: c}
		reqData = make(map[string]interface{})
	)

	httpCode, errCode := rsp.ParamParse(&reqData)

	if httpCode != http.StatusOK {
		rsp.SendJSON(httpCode, errCode, nil)
		return
	}

	// 发送短信测试
	regionId := gconfig.AliyunConfig.RegionId
	accessKeyId := gconfig.AliyunConfig.AccessKeyId
	accessKeySecret := gconfig.AliyunConfig.AccessKeySecret

	sms := gsms.NewSms(regionId, accessKeyId, accessKeySecret)

	content := map[string]string{
		"to":             reqData["to"].(string),
		"signName":       reqData["signName"].(string),
		"templatecode":   reqData["templateCode"].(string),
		"templateParams": reqData["templateParams"].(string),
		"showNumber":     reqData["showNumber"].(string),
	}

	sms.SetParams(content)
	sms.Send()

	res := "发送成功！"
	rsp.SendJSON(http.StatusOK, gerrno.SUCCESS, res)
}
